package com.conversationboard.controller;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.conversationboard.config.Configuration;
import com.conversationboard.model.Board;
import com.conversationboard.model.User;

@WebServlet(name = "PrePreferencesControllerServlet", urlPatterns = "/PrePreferencesControllerServlet")
public class PrePreferencesControllerServlet extends HttpServlet {

	private static final long serialVersionUID = -6315690684066263368L;


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		try {
			request.setCharacterEncoding("UTF-8");
			response.setCharacterEncoding("UTF-8");

			User user = User.get(request.getUserPrincipal().getName());

			List<Board> subscriptions = user.getSubscriptions();

			request.setAttribute("subscriptions", subscriptions);
			request.setAttribute("config", Configuration.getInstance());

			RequestDispatcher dispatcher = request.getRequestDispatcher("/Pages/preferences.jsp");
			dispatcher.forward(request, response);

		} catch (SQLException e) {
			throw new ServletException(e);
		}

	}

}
